﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sim;
using Users;
using System.Xml.Serialization;
using System.IO;
namespace Lists
{
    static public class TournamentRegisterList
    {
        static public List<Registration> tournamentRegistrationList { get; set; }

        static TournamentRegisterList()
        {
            tournamentRegistrationList = new List<Registration>();
        }

        static public void AddRegistration(Registration r)
        {
            tournamentRegistrationList.Add(r);
            SerializeToXML();
        }

        static public void RemoveRegistration(string username, string tournamentName)
        {
            for (int i = 0; i < tournamentRegistrationList.Count; i++)
            {
                if (tournamentRegistrationList[i].username == username && tournamentRegistrationList[i].tournamentName == tournamentName)
                {
                    tournamentRegistrationList.RemoveAt(i);
                    SerializeToXML();
                    break;
                }
            }
            
        }

        static public void SerializeToXML()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List<Registration>), new XmlRootAttribute("REGISTRATIONS"));
            TextWriter textWriter = new StreamWriter("registrations.xml");
            serializer.Serialize(textWriter, tournamentRegistrationList);
            textWriter.Close();
        }

        static public void DeserializeFromXML()
        {
            tournamentRegistrationList.Clear();
            XmlSerializer deserializer = new XmlSerializer(typeof(List<Registration>), new XmlRootAttribute("REGISTRATIONS"));
            TextReader textReader = new StreamReader("registrations.xml");
            tournamentRegistrationList = (List<Registration>)deserializer.Deserialize(textReader);
            textReader.Close();
        }
    }
}
